package com.yunzheng.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzheng.entity.ProjectMission;
import com.yunzheng.mapper.ProjectMissionMapper;
import com.yunzheng.mapper.SysUserMapper;
import com.yunzheng.service.ProjectMissionService;
import com.yunzheng.vo.MissionGanttchartVo;
import com.yunzheng.vo.MissionShowListVo;
import com.yunzheng.vo.MissionShowVo;
import com.yunzheng.vo.MyMissionListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李美琪
 * @date 2020/7/27 21:38
 */

@Service
public class ProjectMissionServiceImpl extends ServiceImpl<ProjectMissionMapper, ProjectMission> implements ProjectMissionService {

    @Resource
    private ProjectMissionMapper projectMissionMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /*
     * 新建工作项
     */
//    @Override
//    public void missionPost(String missionTitle, String missionContent, String missionProject, String missionManager, Integer missionPriority, Integer missionType, LocalDateTime startTime, LocalDateTime finishTime) {
//        ProjectMission entity = new ProjectMission();
//
//        // 雪花算法生成工作项id
//        String missionId = SnowFlakeUtil.nextId();
////        //判断项目负责人和所属项目是否存在
////        Integer inProject = projectMapper.selectCount(new QueryWrapper<Project>().eq("project_id",missionProject).last("limit 1"));
////        Integer inManager = sysAuthMapper.selectCount(new QueryWrapper<SysAuth>().eq("userId",missionManager).last("limit 1"));
//        //计算结束时间和开始时间的时间差
//        long time = Duration.between(startTime,finishTime).toDays();
//        if(time < 0){
//            throw new ServiceException("结束时间早于开始时间");
//        }
//        entity.setMissionId(missionId).setMissionTitle(missionTitle).setMissionContent(missionContent).setMissionProject(missionProject).setMissionManager(missionManager).setMissionPriority(missionPriority).setMissionType(missionType).setStartTime(startTime).setFinishTime(finishTime);
//        projectMissionMapper.insert(entity);
//
//    }

    /**
     * 工作项甘特图
     * @param missionProject 工作项所属的项目
     * @return 返回项目工作项甘特图
     */
    @Override
    public List<MissionGanttchartVo> missionGanttchart(String missionProject) {

        return projectMissionMapper.getMissionGanttchart(missionProject);

        //不使用xml，两表联查返回以MissionGanttchartVo为泛型的list列表，两表查询出的list横向合并
//        List<ProjectMission> projectMission = projectMissionMapper.selectList(new QueryWrapper<ProjectMission>().eq("projectId",projectId).select("missionId","missionTitle","managerId","missionStatus","missionProgress","missionType","startTime","finishTime"));
//        List<String> managerIds = projectMission.stream().map(ProjectMission::getManagerId).collect(Collectors.toList());
//        List<SysUser> sysUsers = sysUserMapper.selectList(new QueryWrapper<SysUser>().select(SysUser.COL_REALNAME).in(SysUser.COL_USER_ID, managerIds));

//        List<MissionGanttchartVo> missionGanttchartVo = projectMission.stream().flatMap(
//                x -> sysUsers.stream().filter(y -> y.getUserId().equals(x.getManagerId())).map(y -> new MissionGanttchartVo(x.getMissionId(),x.getMissionTitle()
//                        ,x.getManagerId(),y.getRealname(),x.getMissionStatus(),x.getMissionProgress(),x.getMissionType(),x.getStartTime(),x.getFinishTime()))
//        ).collect(Collectors.toList());
//        return missionGanttchartVo;

//        新增返回数据的自定义一列的SQL语句
//        SELECT  TIMESTAMPDIFF(DAY,current_timestamp(),sys_user.update_time) AS 'rest_time' from sys_user;


    }

    /**
     * 展示工作项列表
     * @param missionProject 要展示工作项的项目ID
     * @param missionType 工作项的类型（需求、缺陷、任务）
     * @return 返回工作项的基本信息
     */
    @Override
    public List<MissionShowListVo> missionShowList(String missionProject, Integer missionType) {
        List<MissionShowListVo> missionShowList = projectMissionMapper.getMissionShowList(missionProject, missionType);
        // 如果剩余时间为负数，就应该将其置0
        for (MissionShowListVo vo : missionShowList){
            if (Integer.parseInt(vo.getRestTime()) < 0){
                vo.setRestTime("0");
            }
        }
        return missionShowList;
    }

    /**
     * 显示工作项内容
     */
    @Override
    public MissionShowVo missionShow(String missionId) {
        return projectMissionMapper.getMissionShow(missionId);
    }


    /**
     * 显示我的工作项，根据userId
     * @param userId 根据token的用户id
     * @return 返回 该用户的工作项
     */
    @Override
    public List<MyMissionListVO> myMission(String userId) {
        String realname = sysUserMapper.selectById(userId).getRealname();
        return projectMissionMapper.getMyMissionList(realname);
    }

//    /**
//     * 修改工作项
//     */
//    @Override
//    public void missionChange(String missionId, String missionTitle, String missionContent, String missionManager, Integer missionPriority, Integer missionType, LocalDateTime startTime, LocalDateTime finishTime) {
//        //通过missionId寻找到需要修改的工作项
//        ProjectMission entity = projectMissionMapper.selectById(missionId);
//        LocalDateTime dateTime = LocalDateTime.now();
//        long time = Duration.between(startTime,finishTime).toDays();
//        if(time<0){
//            throw new ServiceException("结束时间早于开始时间");
//        } else {
//            entity.setMissionTitle(missionTitle).setMissionContent(missionContent).setMissionManager(missionManager).setMissionPriority(missionPriority).setMissionType(missionType).setStartTime(startTime).setFinishTime(finishTime).setUpdateTime(dateTime);
//            projectMissionMapper.updateById(entity);
//        }
//    }

//    /**
//     * 删除工作项
//     */
//    @Override
//    public void missionRemove(String missionId) {
//        if (missionId == null) {
//            throw new ServiceException("无删除项");
//        } else {
//            projectMissionMapper.deleteById(missionId);
//        }
//    }
//
//    /**
//     * 批量删除工作项
//     */
//    @Override
//    public void missionRemoveList(List<String> missionId) {
//        if (missionId.size() == 0) {
//            throw new ServiceException("未选择删除项");
//        } else {
//            projectMissionMapper.deleteBatchIds(missionId);
//        }
//    }

}


